package com.yanwq.sayhi.controller.user.activity;

import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.EditText;

import com.alibaba.fastjson.JSONObject;
import com.yanwq.sayhi.R;
import com.yanwq.sayhi.base.BaseToolbarActivity;
import com.yanwq.sayhi.common.client.OkJsonObjectResponseCallback;
import com.yanwq.sayhi.common.client.OkRestClient;
import com.yanwq.sayhi.common.constant.RegexConstant;
import com.yanwq.sayhi.common.tool.MD5Tool;
import com.yanwq.sayhi.controller.user.UserMgr;
import com.yanwq.sayhi.dao.bean.User;
import com.yanwq.sayhi.widget.MToast;

import java.util.regex.Pattern;

import butterknife.BindView;

public class PasswordChangeActivity extends BaseToolbarActivity {

    @BindView(R.id.password_old)
    EditText mOldV;
    @BindView(R.id.password_new)
    EditText mNewV;
    @BindView(R.id.password_confirm)
    EditText mConfirmV;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_password_change);
    }

    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_send, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        updatePassword();
        return true;
    }

    @Override
    public void init() {
    }

    @Override
    public void initView() {
    }

    private void updatePassword() {
        if (!Pattern.matches(RegexConstant.password, mOldV.getText().toString())) {
            MToast.showShort(this, "原密码格式错误");
            return;
        }

        if (!Pattern.matches(RegexConstant.password, mNewV.getText().toString())) {
            MToast.showShort(this, "新密码格式错误");
            return;
        }

        if (!Pattern.matches(RegexConstant.password, mConfirmV.getText().toString())) {
            MToast.showShort(this, "确认密码格式错误");
            return;
        }

        if (!mNewV.getText().toString().equals(mConfirmV.getText().toString())) {
            MToast.showShort(this, "两次密码不一致");
            return;
        }

        if (!MD5Tool.GetMD5Code(mOldV.getText().toString()).equals(UserMgr.get().getPassword())) {
            MToast.showShort(this, "原密码错误");
            return;
        }

        User user = UserMgr.get();
        user.setPassword(mConfirmV.getText().toString());

        OkRestClient.getInstance().post(this,
                "/user/update",
                user,
                new OkJsonObjectResponseCallback() {
                    @Override
                    public void onStart() {
                        showProgressDialog("正在修改密码");
                    }

                    @Override
                    public void onSuccess(JSONObject jsonObject) {
                        UserMgr.update(jsonObject);
                        MToast.showShort(PasswordChangeActivity.this, "恭喜，密码修改成功");
                        delayFinish();
                    }

                    @Override
                    public void onStop() {
                        cancelProgressDialog();
                    }
                });
    }
}
